/**我的订单页面 */
var basePage = require('../../../common/BasePage');
var baseOrder = require('../../../common/Order');
Page({
    behaviors: [basePage, baseOrder],
    data: {
        active: 0,
        from: 'flower',
        orderArr: [{ //订单列表数据
                title: '全部',
                type: 0,
                current_page: 0,
                pages: 1,
                data: [],
                loading: false,
                finished: false,
            },
            {
                title: '待付款',
                type: 1,
                loading: false,
                current_page: 0,
                pages: 1,
                finished: false,
                data: [],
            },
            {
                title: '待发货',
                type: 2,
                loading: false,
                current_page: 0,
                pages: 1,
                finished: false,
                data: [],

            },
            {
                title: '待收货',
                type: 3,
                loading: false,
                current_page: 0,
                pages: 1,
                finished: false,
                data: [],

            },
            {
                title: '待评论',
                type: 4,
                loading: false,
                finished: false,
                current_page: 0,
                pages: 1,
                data: [],
            },
            {
                title: '已完成',
                type: 5,
                loading: false,
                finished: false,
                current_page: 0,
                pages: 1,
                data: [],
            }
        ],
        showPay: false,
        orderInfo: {},
        /**支付方式 */
        pay: 'coin',
    },
    tab_change(event) {
        console.log("tab_change", event)
        let active = parseInt(event.detail.name);
        this.data.orderArr[active].current_page = 0
        this.setData({
            active
        })
        this.req_orders();
    },
    onLoad: function (options) {
        if (options.active) {
            this.setData({
                active: parseInt(options.active)
            });
        }
        if (options.from) {
            this.data.from = options.from;

        }
        // this.req_orders();
    },
    onShow: function () {
        this.data.orderArr[this.data.active].current_page = 0;
        this.req_orders();
    },
    //拆单
    split_order(e) {
        console.log("拆单", e)
        var orderProduct = e.currentTarget.dataset.order.order.order_product
        if (orderProduct.length <= 1 && orderProduct[0].real_product_number <= 1) {
            wx.showToast({
                title: '只有一笔订单明细，无法进行拆单！',
                icon: 'none',
            })
            return;
        }
        wx.navigateTo({
            url: '/pages/splitOrder/splitOrder?orderId=' + e.currentTarget.dataset.order.order_id,
            success: function (obj) {
                //传递产品数据
                obj.eventChannel.emit('orderDetail', e.currentTarget.dataset.order)
            }
        })
    },
    //批量修改物流
    order_change(e) {
        console.log("批量选择物流", e)
        var orderArr = this.data.orderArr
        var index = e.currentTarget.dataset.index
        orderArr[this.data.active].data[index].selected = !orderArr[this.data.active].data[index].selected
        console.log("order", orderArr)
        this.setData({
            orderArr: orderArr
        })
    },
    //选择物流
    click_logistics() {
        var order = this.data.orderArr[this.data.active].data
        var orderId = []
        order.map(val => {
            if (val.selected) {
                orderId.push(val.order_id)
            }
        })
        if (orderId.length <= 0) {
            wx.showToast({
                title: '请至少选择一条订单',
                icon: 'none'
            })
            return;
        }

        wx.navigateTo({
            // url: '/pages/order_confirm/order_confirm',
            url: '/pages/logistics/logistics/logistics',
            success: function (obj) {
                //传递产品数据
                obj.eventChannel.emit('orderDetail', orderId)
            }

        })
    },
    login_reload() {
        this.req_orders();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        // console.log("触底");
        this.req_orders()
    },
    /**下拉刷新 */
    onPullDownRefresh() {
        this.data.orderArr[this.data.active].current_page = 0;
        this.req_orders()
    },
    /**
     * 去评价
     */
    comment_click(e) {
        let order = e.currentTarget.dataset.order;

        // wx.navigateTo({
        //     url: '/pages/cls/comment/Order',
        //     success: function (obj) {
        //         //传递订单数据
        //       obj.eventChannel.emit('orderData', order)
        //     }
        //   })
        //   return;
        if (order.order.order_product.length > 1) {
            wx.navigateTo({
                url: '/pages/cls/comment/Order',
                success: function (obj) {
                    //传递订单数据
                    obj.eventChannel.emit('orderData', order)
                }
            })
        } else {
            wx.navigateTo({
                url: '/pages/cls/myOrder_evaluate/evaluate',
                success: function (obj) {
                    //传递产品数据
                    obj.eventChannel.emit('pdcData', order.order.order_product[0])
                }
            })
        }

    },
    complaint_click(e) {
        let order = e.currentTarget.dataset.order;
        // if(order.wh_id!=1&&order.wh_id!=4){
        // 	return wx.showToast({
        // 	  title: '平台只处理昆明仓发货售后，请联系分仓',
        // 	  icon: 'none'
        // 	})
        // }
        wx.navigateTo({
            url: '/pages/cls/comment/Order?type=complaint',
            success: function (obj) {
                //传递订单数据
                obj.eventChannel.emit('orderData', order)
            }
        })
        return;
    },
    /**取消订单 */
    cancel_click(e) {
        console.log("cancel_click", e);
        let order = e.currentTarget.dataset.order;
        let index = e.currentTarget.dataset.index;
        let active = this.data.active;
        this.request({
            url: this.data.host + '/home/order/cancel',
            data: {
                order_id: order.order_id,
            },
            method: "POST",
        }, res => {
            // console.log('取消订单成功' , res);
            wx.showToast({
                title: '取消成功',
                icon: 'none'
            })
            order.order_status = -1;
            order.order_status_label = "已取消";
            this.data.orderArr[active].data[index] = order;
            this.setData({
                orderArr: this.data.orderArr
            })
        })
    },
    /**
     * 确认收货
     */
    finished_click(e) {
        console.log("finished_click", e);
        let order = e.currentTarget.dataset.order;
        let index = e.currentTarget.dataset.index;
        let active = this.data.active;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '确认已收到货物？',
            success(res) {
                if (res.confirm) {
                    // console.log('用户点击确定')
                    that.request({
                        url: that.data.host + '/home/order/finished',
                        data: {
                            order_id: order.order_id,
                        },
                        method: "POST",
                    }, res => {
                        if (active != 0) {
                            that.data.orderArr[active].data.splice(index, 1);
                        } else {
                            order.order_status = 8;
                            order.order_status_label = "待评价";
                            that.data.orderArr[active].data[index] = order;
                        }
                        that.setData({
                            orderArr: that.data.orderArr
                        })
                    })
                }
            }
        })
    },
    /**跳转支付页面 */
    pay_click(e) {
        var orderInfo = e.currentTarget.dataset.order
        console.log("")
        //orderInfo.wh_id == 5 || orderInfo.wh_id == 7
        if(orderInfo.from=='wh_pin'){
            //分仓拼单使用总仓支付
            this.getPayType(1)
        }else{
            this.getPayType(orderInfo.wh_id)
        }
        
        this.setData({
            orderInfo: orderInfo
        })

    },
    successPayOrderWh() {
        this.setData({
            showPay: true
        })
    },
    /**
     * 请求订单列表
     */
    req_orders() {
        /**
         * 当前状态的订单数据
         */
        let data = this.data.orderArr[this.data.active];
        let that = this;
        if (data.pages > data.current_page) {
            this.request({
                url: this.data.host + '/home/order/index?type=' + data.type + '&from=' + this.data.from,
                data: {
                    page: data.current_page + 1,
                }
            }, res => {
                // console.log("/home/order/index", res);
                data.current_page = res.current_page;
                data.pages = res.pages;
                res.data.map(function (val) {
                    if (val.order) {
                        val.order.order_product.map(function (pdc) {
                            pdc.thumb = that.get_thumb(pdc.product_img);
                        });
                    }


                })
                data.current_page > 1 ? data.data = data.data.concat(res.data) : data.data = res.data;

                this.setData({
                    orderArr: this.data.orderArr
                })
            })
        }
    },
    onClick(event) {
        let name = event.currentTarget.dataset.name;
        // this.data.payData.code = name;
        this.setData({
            pay: name,
        })
    },
    onClose() {
        this.setData({
            showPay: false
        })
    },
    pay_ok() {
        var param = {
            code: this.data.pay,
            order_id: this.data.orderInfo.order_id
        }
        this.setData({
            showPay: false
        })
        this.request({
            url: this.data.host + '/home/payment/order',
            loadingTip: "正在提交",
            method: "POST",
            data: param
        }, res => {

            //  console.log("payment/order",res,this.data.payData.code);
            if (this.data.pay == 'wxpay') { //调用微信支付
                wx.requestPayment({
                    ...res.data.pay_data,
                    success(res) {
                        wx.showToast({
                            title: '支付成功',
                        })

                    },
                    fail(err) {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none'
                        })
                        console.error('pay fail', err)
                    },
                    complete() {

                        this.data.orderArr[this.data.active].current_page = 0
                        this.req_orders();
                    }
                })
            } else if (this.data.pay == 'allin') {
                //通联支付
                var payInfo = res.data.payInfo
                if (payInfo) {
                    payInfo = JSON.parse(payInfo)
                    wx.requestPayment({
                        ...payInfo,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                            })

                        },
                        fail(err) {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none'
                            })
                            console.error('pay fail', err)
                        },
                        complete() {
                            this.data.orderArr[this.data.active].current_page = 0
                            this.req_orders();
                        }
                    })
                } else if (res.data.extraData) { //跳转收银台小程序
                    var extraData = res.data.extraData

                    this.alinePay(extraData, this.pay_suc)

                } else {
                    wx.showToast({
                        title: '支付失败',
                        icon: "none"
                    })
                }

            } else {
                this.data.orderArr[this.data.active].current_page = 0
                this.req_orders();
            }
            //  return;

        })
    },
    pay_suc() {
        wx.showToast({
            title: '支付成功',
        })
        this.data.orderArr[this.data.active].current_page = 0
        this.req_orders();
    },
    stop() {},
    //查看详情
    toDetail(e) {
        var order = e.currentTarget.dataset.info
        wx.navigateTo({
            url: '/pages/cls/my_order/orderDetail?orderId=' + order.order_id,
        })
    },
    //查看物流
    shipping_click(e) {
        var order = e.currentTarget.dataset.order

        wx.navigateTo({
            url: '/pages/profit/today_check/check?tabIndex=1&wh_id=' + order.wh_id + "&day=" + order.day_label,
        })
    }

})